//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Copyright 2012 Microsoft Corporation. All Rights Reserved
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.   
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

namespace PreflightClientTool
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Preflight.ServiceModel.Client;
    using Preflight.ServiceModel.Client.Storage;

    public static class PreflightFacade
    {
        private static IEndpointDescriptionRepository provider;

        public static void DeleteTarget(Target target)
        {
            provider.Remove(target.Endpoint);
        }

        public static void UpsertTarget(Target target)
        {
            provider.Upsert(target.Endpoint);
        }

        public static IEnumerable<Target> GetTargets(string storePath)
        {
            provider = EndpointDescriptionXmlProvider.CreateSecure(storePath);
            return from endpoint in provider.GetEndpoints()
                   select new Target
                       {
                           Endpoint = endpoint,
                           State = TargetState.Unknown
                       };
        }

        public static void RunPreflightChecks(Target target, Action<Target> onCompleted)
        {
            Task.Factory
                .StartNew(() => RunPreflightCheck(target))
                .ContinueWith(task => onCompleted(target));
        }

        private static void RunPreflightCheck(Target target)
        {
            var client = ClientFactory.Create().CreateClient(target.Endpoint);
            if (client == null)
            {
                target.State = TargetState.Error;
                target.Error = "There is no client available for this endpoint";
                return;
            }

            try
            {
                target.Check = client.RunPreflightCheck(target.Endpoint);
                target.State = target.Check.Passed ? TargetState.Passed : TargetState.Failed;
            }
            catch (Exception exception)
            {
                target.Error = exception.ToString();
                target.State = TargetState.Error;
            }
        }
    }
}
